import axios from "axios";
// import { Message } from "element-ui";
axios.defaults.withCredentials = true; // 让ajax携带cookie

const instance = axios.create({
  // baseURL 将自动加在 url`前面，除非 url 是一个绝对 URL。
  // 它可以通过设置一个 baseURL 便于为 axios 实例的方法传递相对 URL

  baseURL: "https://lapp.xiaoshouyi.com",
  // timeout设置一个请求超时时间，如果请求时间超过了timeout，请求将被中断，单位为毫秒（ms）
  timeout: 10000,
  withCredentials: true,
  // headers是被发送的自定义请求头，请求头内容需要根据后端要求去设置，这里我们使用本项目请求头。
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",

    // Cookie:
    //   "x-ienterprise-tenant=3087824860205395; c-full-server-name=crm.xiaoshouyi.com; c-user=3087832097194195; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2230878320971941953087824860205395%22%2C%22first_id%22%3A%2218e1bb21d043b4-0b4b6e322581288-26001b51-1327104-18e1bb21d05cd2%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218e1bb21d043b4-0b4b6e322581288-26001b51-1327104-18e1bb21d05cd2%22%7D; creekflowG6Designer=true; introduceNewEditionV2312_3087832097194195=0; languageCode=zh; flowForceTrance2English=false; deploy.hash=240304152009; deploy.isdev=false; svg_file=sprite-0.1.14.svg; resDomain=https://rs.ingageapp.com; JSESSIONID=BC5F84150CEF6D79C3AE3CEDD8557DB4; JSESSIONID=543021532631DF2E59DB4905AAE02549; LAPP_SESSION_ID=543021532631DF2E59DB4905AAE02549",
  },
});

// http request 请求拦截器
instance.interceptors.request.use(
  (config) => {
    // 这里判断localStorage里面是否存在token，如果有则在请求头里面设置
    // if (localStorage.jwtToken) {
    //   config.headers.Authorization = getLocalStorage("jwtToken");
    // }

    // config.headers["Cookie"] =
    //   "x-ienterprise-tenant=3087824860205395; c-full-server-name=crm.xiaoshouyi.com; c-user=3087832097194195; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2230878320971941953087824860205395%22%2C%22first_id%22%3A%2218e1bb21d043b4-0b4b6e322581288-26001b51-1327104-18e1bb21d05cd2%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218e1bb21d043b4-0b4b6e322581288-26001b51-1327104-18e1bb21d05cd2%22%7D; creekflowG6Designer=true; introduceNewEditionV2312_3087832097194195=0; languageCode=zh; flowForceTrance2English=false; deploy.hash=240304152009; deploy.isdev=false; svg_file=sprite-0.1.14.svg; resDomain=https://rs.ingageapp.com; JSESSIONID=BC5F84150CEF6D79C3AE3CEDD8557DB4; JSESSIONID=543021532631DF2E59DB4905AAE02549; LAPP_SESSION_ID=543021532631DF2E59DB4905AAE02549";

    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// http response 响应拦截器
instance.interceptors.response.use(
  (response) => {
    // 这里对响应的数据做了操作，大家可以自己设置响应过滤哦，下面会给出具体代码
    return response.data;
  },
  (error) => {
    // 错误信息需要和后端进行协调，然后去配合设置啦
    return Promise.reject(error);
  }
);

export default instance;
